//
//  DocFileListDisplayController.m
//  MClient
//
//  Created by  史正烨 on 10-8-11.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DocFileListDisplayController.h"
#import "MClientAppDelegate.h"
#import "EditFormController.h"
#import "FileListViewController.h"
#import "MServerDAO.h"


@implementation DocFileListDisplayController
@synthesize isMultiFile;

#pragma mark -
#pragma mark View lifecycle

// 得到文件的存储路径
- (NSString *) dataFilePath {

	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
														 , NSUserDomainMask, YES);
	return [paths objectAtIndex:0];
}


- (void)viewDidLoad {
    [super viewDidLoad];

	self.tableview.backgroundColor = [UIColor clearColor];
	
	fileList = [[NSMutableDictionary alloc] init];
	keys = [[NSMutableArray alloc] init];
	
	NSArray* extList = [NSArray arrayWithObjects:@"docx", @"pptx", @"xlsx", @"doc",
						        @"pdf" , @"ppt" ,@"rtf" ,@"txt" ,@"xls" ,@"zip" ,
						@"Docx", @"Pptx", @"Xlsx", @"Doc",
						@"Pdf" ,@"Ppt" ,@"Rtf" ,@"Txt" ,@"Xls" ,@"Zip" ,
						@"DOCX", @"PPTX", @"XLSX", @"DOC",
						@"PDF" ,@"PPT" ,@"RTF" ,@"TXT" ,@"XLS" ,@"ZIP" ,nil];
	
	NSArray* files = [[[[NSFileManager defaultManager] directoryContentsAtPath:[self dataFilePath]]
				  pathsMatchingExtensions:extList] retain] ;
	
	for (NSString* file in files) {
		NSString *key = [[file pathExtension] lowercaseString];

		NSMutableArray* value = [fileList valueForKey:key];
		
		if (value == nil) {
			value = [[NSMutableArray alloc] init];
			[fileList setValue:value forKeyPath:key];
			[keys addObject:key];
		}
		[value addObject:file];
	}
}



#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return [keys count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	NSString *key = (NSString*)[keys objectAtIndex:section];
	NSArray *files = (NSArray*)[fileList objectForKey:key];
	
	return [files count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.textLabel.font = [UIFont systemFontOfSize:16];
		cell.textLabel.backgroundColor = [UIColor clearColor];
    }
	
    
    // Configure the cell...
	NSString *key = (NSString*)[keys objectAtIndex:indexPath.section];
	NSArray *files = (NSArray*)[fileList objectForKey:key];
	
    cell.textLabel.text = (NSString*)[files objectAtIndex:indexPath.row];
	if ([key isEqual:@"plist"]) {
		cell.imageView.image = [UIImage imageNamed:[NSString stringWithString:@"file-unknow.png"]];
	} else {
		cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"file-%@.png", key]];
	}
	
    return cell;
}



#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.

	NSString *key = (NSString*)[keys objectAtIndex:indexPath.section];
	NSArray *files = (NSArray*)[fileList objectForKey:key];
	
	NSString *szFilePath;

	NSString* szPath   = [self dataFilePath];
	NSString *szFileName = (NSString*)[files objectAtIndex:indexPath.row];
	
	szFilePath = [szPath stringByAppendingPathComponent:szFileName];
	if (isMultiFile) {
		FileListViewController *fileListView = [self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count] - 2];
		
		[fileListView getUploadFileName];
		
		fileListView.cacheFile = szFileName;
		
		fileListView.fileData = [NSInputStream inputStreamWithFileAtPath:szFilePath];
		
	}else {
		EditFormController *editFormController = [self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count] - 2];

		[editFormController getUploadFileName];
		
		editFormController.cacheFile = szFileName;
		
		editFormController.fileData = [NSInputStream inputStreamWithFileAtPath:szFilePath];
	}
	[self.navigationController popViewControllerAnimated:YES];
}


- (void)dealloc {
	[keys release];
	[fileList release];
    [super dealloc];
}


@end

